package cool.taomu.toolkit.jooq

import com.google.inject.Key
import cool.taomu.toolkit.Context
import cool.taomu.toolkit.TaomuConfigure
import cool.taomu.toolkit.TaomuGuice
import cool.taomu.toolkit.TaomuGuiceServiceLoader
import cool.taomu.toolkit.annotations.datasource.Ds
import cool.taomu.toolkit.annotations.datasource.Primary
import cool.taomu.toolkit.crypto.Base64
import javax.inject.Inject
import javax.sql.DataSource
import org.jooq.DSLContext
import org.junit.jupiter.api.Test

class JooqTest {

    @Inject
    DSLContext dsl

    @Ds("taomu.jdbc.name.name1")
    def d() {
        var sql = "select count(1) from taomu_stock_history";
        var Long count = dsl.fetchOne(sql).get(0, Long);
        return count;
    }

    @Test
    def void a() {
        val TaomuConfigure config = new TaomuConfigure(new Base64())
        config.loadConfig
        var injector = TaomuGuice.createInjector(config)
        TaomuGuiceServiceLoader.loadIPlatform(injector, config)
        var context = injector.getInstance(Context);
        var test = context.supplierInjector.get().getInstance(Key.get(DataSource, Primary))
        var testj = context.supplierInjector.get().getInstance(JooqTest)
        if (test !== null) {
            var sql = "select count(1) from taomu_stock_history";
            var Long count = testj.dsl.fetchOne(sql).get(0, Long);
            System.out.println("Count: " + count);
        } else {
            println("DataSource = null")    
        }
        println("aaaa"+testj.d());
    }
}
